import api from '@/api';
import type { AxiosPromise } from 'axios';

export interface CardItem {
  id: number;
  card_json: {
    cardId: string;
    maskCardNo: string;
    cardNo: string;
    firstName: string;
    lastName: string;
    email: string;
    cardFormFactor: string;
    cardType: string;
    nickname: string;
    cardBalance: number;
    cardCurrency: string;
    cardStatus: string;
    cardScheme: string;
  };
  created_at: string;
}

export interface CardListResponse {
  items: CardItem[];
  total: number;
}

export interface CardListRequest {
  _action: string;
  page: number;
  perPage: number;
  cardType?: string;
  cardFormFactor?: string;
  cardStatus?: string;
  createdAtStart?: string;
  createdAtEnd?: string;
}

export interface CardTypeOption {
  value: string;
  label: string;
  disable?: boolean;
}

export interface CardFormFactorOption {
  value: string;
  label: string;
  disable?: boolean;
}

export interface CardStatusOption {
  value: string;
  type: string;
  label: string;
}

export interface CardBinListType {
  value: string;
  label: string;
}

export interface CardHolderListType {
  value: string;
  label: string;
}

export interface HolderDetails {
  id: number;
  first_name: string;
  last_name: string;
  email: string;
  mobile_number: string;
  country_code: string;
  billing_address: string;
  billing_city: string;
  billing_country: string;
  postal_code: string;
  billing_state: string;
  status: string;
  created_at: string;
}

export interface CreateCardRequest {
  cardBin: string;
  cardCurrency: string;
  cardFormFactor: string;
  cardScheme: string;
  cardType: string;
  cardholderId: string;
  rechargeAmount: number;
}

export interface CardFeesData {
  card_opening_fee: string;
  card_service_fee: string;
  available_balance: string;
}

// 获取卡片列表
export const getCardList = (params: CardListRequest): AxiosPromise<CardListResponse> => {
  return api.get('api/cards', { params });
};

// 获取卡类型选项
export const getCardTypes = (): AxiosPromise<CardTypeOption[]> => {
  return api.get('api/cards', {
    params: {
      _action: 'getData',
      _subAction: 'getCardTypes'
    }
  });
};

// 获取卡介质选项
export const getCardFormFactors = (): AxiosPromise<CardFormFactorOption[]> => {
  return api.get('api/cards', {
    params: {
      _action: 'getData',
      _subAction: 'getCardFormFactors'
    }
  });
};

// 获取卡状态选项
export const getCardStatus = (): AxiosPromise<CardStatusOption[]> => {
  return api.get('api/cards', {
    params: {
      _action: 'getData',
      _subAction: 'getCardStatus'
    }
  });
};

// 获取卡BIN列表
export const queryCardBin = (params: {
  _action: string;
  _subAction: string;
  cardType: string;
  cardScheme: string;
  cardCurrency: string;
  cardFormFactor: string;
}): AxiosPromise<{ options: CardBinListType[] }> => {
  return api.get('api/cards', { params });
};

// 获取持卡人列表
export const queryCardHolder = (params: {
  _action: string;
  _subAction: string;
}): AxiosPromise<{ options: CardHolderListType[] }> => {
  return api.get('api/cards', { params });
};

// 获取卡片费用信息
export const getCardFees = (): AxiosPromise<CardFeesData> => {
  return api.get('api/cards', {
    params: {
      _action: 'getData',
      _subAction: 'getCardFees'
    }
  });
};

// 创建卡片
export const createCard = (data: CreateCardRequest): AxiosPromise<{ msg: string; status: number }> => {
  return api.post('api/cards', data, {
    params: {
      _action: 'createCard'
    }
  });
};

// 编辑卡片昵称
export const editCardNickname = (data: {
  id: number;
  card_json: {
    nickname: string;
  };
}): AxiosPromise<{ msg: string; status: number }> => {
  return api.post('api/cards', data, {
    params: {
      _action: 'editCardNickname'
    }
  });
};

// 获取卡片详情
export const getCardDetails = (params: {
  id: number;
}): AxiosPromise<CardItem> => {
  return api.get('api/cards', { params });
};

// 获取持卡人详情
export const getHolderDetails = (params: {
  id: number;
}): AxiosPromise<HolderDetails> => {
  return api.get('api/cards', { params });
};

export default {
  getCardList,
  getCardTypes,
  getCardFormFactors,
  getCardStatus,
  queryCardBin,
  queryCardHolder,
  getCardFees,
  createCard,
  editCardNickname,
  getCardDetails,
  getHolderDetails
};